package com.cinema.model;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Objects;
import java.util.UUID;

/**
 * 电影票类
 * 
 * @author Cinema Management System
 * @version 1.0.0
 */
public class Ticket {
    
    /**
     * 电子票号
     */
    private String ticketId;
    
    /**
     * 排片ID
     */
    private String scheduleId;
    
    /**
     * 座位ID
     */
    private String seatId;
    
    /**
     * 票价
     */
    private BigDecimal price;
    
    /**
     * 折扣率
     */
    private BigDecimal discountRate;
    
    /**
     * 实际支付金额
     */
    private BigDecimal actualPrice;
    
    /**
     * 购票时间
     */
    private LocalDateTime purchaseTime;
    
    /**
     * 票务状态
     */
    private TicketStatus status;
    
    /**
     * 默认构造函数
     */
    public Ticket() {
        this.ticketId = generateTicketId();
        this.purchaseTime = LocalDateTime.now();
        this.status = TicketStatus.VALID;
        this.discountRate = BigDecimal.ONE;
    }
    
    /**
     * 构造函数
     * 
     * @param scheduleId 排片ID
     * @param seatId 座位ID
     * @param price 票价
     */
    public Ticket(String scheduleId, String seatId, BigDecimal price) {
        this();
        this.scheduleId = scheduleId;
        this.seatId = seatId;
        this.price = price;
        calculateActualPrice();
    }
    
    /**
     * 生成电子票号
     * 
     * @return 电子票号
     */
    private String generateTicketId() {
        return "T" + UUID.randomUUID().toString().replace("-", "").substring(0, 15).toUpperCase();
    }
    
    /**
     * 计算实际支付金额
     */
    private void calculateActualPrice() {
        if (price != null && discountRate != null) {
            this.actualPrice = price.multiply(discountRate);
        }
    }
    
    /**
     * 设置折扣率
     * 
     * @param discountRate 折扣率
     */
    public void setDiscountRate(BigDecimal discountRate) {
        if (discountRate.compareTo(BigDecimal.ZERO) <= 0 || discountRate.compareTo(BigDecimal.ONE) > 0) {
            throw new IllegalArgumentException("折扣率必须在0-1之间");
        }
        this.discountRate = discountRate;
        calculateActualPrice();
    }
    
    /**
     * 退票
     */
    public void refund() {
        this.status = TicketStatus.REFUNDED;
    }
    
    /**
     * 检查是否可以退票
     * 
     * @return 是否可以退票
     */
    public boolean canRefund() {
        return status == TicketStatus.VALID;
    }
    
    // Getter和Setter方法
    public String getTicketId() {
        return ticketId;
    }
    
    public void setTicketId(String ticketId) {
        this.ticketId = ticketId;
    }
    
    public String getScheduleId() {
        return scheduleId;
    }
    
    public void setScheduleId(String scheduleId) {
        this.scheduleId = scheduleId;
    }
    
    public String getSeatId() {
        return seatId;
    }
    
    public void setSeatId(String seatId) {
        this.seatId = seatId;
    }
    
    public BigDecimal getPrice() {
        return price;
    }
    
    public void setPrice(BigDecimal price) {
        this.price = price;
        calculateActualPrice();
    }
    
    public BigDecimal getDiscountRate() {
        return discountRate;
    }
    
    public BigDecimal getActualPrice() {
        return actualPrice;
    }
    
    public LocalDateTime getPurchaseTime() {
        return purchaseTime;
    }
    
    public void setPurchaseTime(LocalDateTime purchaseTime) {
        this.purchaseTime = purchaseTime;
    }
    
    public TicketStatus getStatus() {
        return status;
    }
    
    public void setStatus(TicketStatus status) {
        this.status = status;
    }
    
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        Ticket ticket = (Ticket) obj;
        return Objects.equals(ticketId, ticket.ticketId);
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(ticketId);
    }
    
    @Override
    public String toString() {
        return "Ticket{" +
                "ticketId='" + ticketId + '\'' +
                ", scheduleId='" + scheduleId + '\'' +
                ", seatId='" + seatId + '\'' +
                ", price=" + price +
                ", discountRate=" + discountRate +
                ", actualPrice=" + actualPrice +
                ", purchaseTime=" + purchaseTime +
                ", status=" + status +
                '}';
    }
} 